<?php

namespace App\Modules\Admin\Http\Controllers;

use App\Models\Icon;
use App\Modules\Admin\Http\Requests\IconCreateRequest;
use App\Modules\Admin\Http\Controllers\BaseController;
use Illuminate\Support\Facades\Cache;
use App\Services\Admin\IconService;
use Illuminate\Http\Request;


class IconController extends BaseController
{
    public function index()
    {
        $param = $this->getParam('Icon','Icon/getList');
        $count = Icon::count();
        return view('admin::icon/index',compact('param','count'));
    }

    /**
     * 获取列表页
     * @param Request $request
     * @method getList
     * @author chengzhigang<1256699215@qq.com>
     */
    public function getList(Request $request){
        Cache::forever('Icon/getList', $request->server->get('QUERY_STRING'));
        $page = $request->input('page', 1);
        $per_page = $request->input('limit', 10);
        $filter = $request->input('filter','');
        $iconService = new IconService();
        $result = $iconService->getList($page,$per_page,$filter);
        if($result['status']==1){
            return layTableApi($result['data']['total'],$result['data']['data']);
        }else{
            return layTableApi(0,[],1);
        }
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin::icon.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param IconCreateRequest $request
     * @return \Illuminate\Http\Response
     */
    public function store(IconCreateRequest $request)
    {
        $data = $request->all();
        Cache::forever('Icon', true);
        if (Icon::create($data)) {
            return apiResponse('添加成功',1,[]);
        }
        return apiResponse('添加失败');
    }


    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request)
    {
        $id = $request->input('id');
        $data = $request->all();
        $icon = Icon::find($id);
        if(empty($icon)){
            return apiResponse('图标不存在');
        }
        if($icon->update($data)){
            return apiResponse('编辑成功',1,[]);
        }
        return apiResponse('编辑失败');
    }


}
